package initAll;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.aventstack.extentreports.ExtentTest;

import javaUnit.ThreadTime;
import seleniumUnit.linkOperate;

public class login {

	private static final Logger log = LogManager.getLogger(login.class.getName());
	private static String url = "http://10.168.1.250:9080/";
	WebDriver driver;
	ExtentTest test;
	linkOperate Operate;

	public login(ExtentTest test, WebDriver driver) {
		this.driver = driver;
		this.test = test;
		PageFactory.initElements(driver, this);
		Operate = new linkOperate(test, driver);
	}

	//form login title  GetText(ch/en)=协同教学管理平台/Ximmerse Operation Platform
	@FindBy(how = How.XPATH, xpath = "//div[@class='login-title']/div")
	WebElement login_title;
	
	//账户名编辑框  GetText(ch/en)=请输入账户名/Please enter account name
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']")
	WebElement edit_UserName;

	//密码编辑框  GetText(ch/en)=请输入密码/Please enter account password
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']")
	WebElement edit_Password;

	//验证码  GetText(ch/en)=请输入密码/Please enter account password
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/div[2]/form/div[3]/div/div/div/input")
	WebElement edit_Key;
	
	//登录按钮  GetText(ch/en)=登录/Login
	@FindBy(how = How.XPATH, xpath = "//button[@class='el-button form-submit el-button--default el-button--large']/span")
	WebElement btn_login;

	//选择英文按钮
	@FindBy(how = How.XPATH, xpath = "//span[text()='English']")
	WebElement btn_language_English;

	//选择中文按钮
	@FindBy(how = How.XPATH, xpath = "//span[text()='中文']")
	WebElement btn_language_chinese;
	
	//登陆成功后的用户名
	@FindBy(how = How.XPATH, xpath = "//div[2]/div/div/div/span[1]")
	WebElement btn_userName;

	//退出登录按钮    GetText(ch/en)=退出登录/Logout
	@FindBy(how = How.XPATH, xpath = "//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']")
	WebElement btn_exit_login;

	//账户名为空提示   GetText(ch/en)=请输入账户名/Please enter account name
	@FindBy(how = How.XPATH, xpath = "//form/div[1]/div/div[2]")
	WebElement table_error_UserName;

	//账户名为空提示   GetText(ch/en)=请输入账户密码/Please enter account password
	@FindBy(how = How.XPATH, xpath = "//form/div[2]/div/div[2]")
	WebElement table_error_password;

	//消息成功  GetText(ch/en)=sucess
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-message__group']")
	WebElement msg_sucess;
	
	//确认按钮  GetText(ch/en)=确定/OK
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-message-box__btns']/button[2]")
	WebElement btn_determine;
	
	//系统管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[1]/div/span")

	public WebElement table_system;
	
	//点击系统管理
	public void table_systemClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_user.isDisplayed())table_system.click();
		//不显示子菜单
		if(!bl&&table_user.isDisplayed())table_system.click();
	}
	
	//用户管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[1]/ul/li[1]/span")
	WebElement table_user;
	
	//点击用户管理
	public void table_userClick(){
		table_user.click();
	}
	
	
	//角色管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[1]/ul/li[2]/span")
	WebElement table_role;
	
	//点击角色管理
	public void table_roleClick(){
		table_role.click();
	}
	
	//内容管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[2]/div/span")
	WebElement table_game;
	public void table_gameClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_gameLS.isDisplayed())table_game.click();
		//不显示子菜单
		if(!bl&&table_gameLS.isDisplayed())table_game.click();
	}
	
	//教学内容
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[2]/ul/li/span")
	WebElement table_gameLS;
	public void table_gameLSClick(){
		table_gameLS.click();
	}
	
	//设备管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[3]/div/span")
	WebElement table_devices;
	public void table_devicesClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_devicesInfo.isDisplayed())table_devices.click();
		//不显示子菜单
		if(!bl&&table_devicesInfo.isDisplayed())table_devices.click();
	}
	
	//设备信息
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[3]/ul/li/span")
	WebElement table_devicesInfo;
	public void table_devicesInfoClick(){
		table_devicesInfo.click();
	}
	
	//教室管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[4]/div/span")
	WebElement table_dept;
	public void table_deptClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_depts.isDisplayed())table_dept.click();
		//不显示子菜单
		if(!bl&&table_depts.isDisplayed())table_dept.click();
	}
	
	//教室管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[4]/ul/li[1]/span")
	WebElement table_depts;
	public void table_deptsClick(){
		table_depts.click();
	}
	
	//教室账号管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[4]/ul/li[2]/span")
	WebElement table_opuser;
	public void table_opuserClick(){
		table_opuser.click();
	}
	
	//版本管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[5]/div/span")
	WebElement table_version;
	public void table_versionClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_upversion.isDisplayed())table_version.click();
		//不显示子菜单
		if(!bl&&table_upversion.isDisplayed())table_version.click();
	}
	
	//版本上传
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[5]/ul/li")
	WebElement table_upversion;
	public void table_upversionClick(){
		table_upversion.click();
	}
	
	//日志管理
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/div/span")
	WebElement table_log;
	public void table_logClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_logRunDevice.isDisplayed())table_log.click();
		//不显示子菜单
		if(!bl&&table_logRunDevice.isDisplayed())table_log.click();
	}
	
	//设备运行
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[1]/span")
	WebElement table_logRunDevice;
	public void table_logRunDeviceClick(){
		table_logRunDevice.click();
	}
	
	//设备异常
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[2]/span")
	WebElement table_logDeviceEx;
	public void table_logDeviceExClick(){
		table_logDeviceEx.click();
	}
	
	//内容开始
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[3]/span")
	WebElement table_startGame;
	public void table_startGameClick(){
		table_startGame.click();
	}
	
	//内容结束
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[4]/span")
	WebElement table_endGame;
	public void table_endaGameClick(){
		table_endGame.click();
	}
	
	//内容时长
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[5]/span")
	WebElement table_gameRuntime;
	public void table_gameRuntimeClick(){
		table_gameRuntime.click();
	}
	
	//内容每局人数
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[6]/span")
	WebElement table_gameplays;
	public void table_gameplaysClick(){
		table_gameplays.click();
	}
	
	//内容异常
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[7]/span")
	WebElement table_gameEX;
	public void table_gameEXClick(){
		table_gameEX.click();
	}
	
	//内容自定义事件
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[8]/span")
	WebElement table_gameEvent;
	public void table_gameEventClick(){
		table_gameEvent.click();
	}
	
	//购买记录
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[9]/span")
	WebElement table_purchase;
	public void table_purchaseClick(){
		table_purchase.click();
	}
	
	//消费记录
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[10]/span")
	WebElement table_consume;
	public void table_consumeClick(){
		table_consume.click();
	}
	
	//赠送记录
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[6]/ul/li[11]/span")
	WebElement table_give;
	public void table_giveClick(){
		table_give.click();
	}
	
	//数据字典
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[7]/div/span")
	WebElement table_data;
	public void table_dataClick(Boolean bl){
		//显示子菜单
		if(bl&&!table_dictCity.isDisplayed())table_data.click();
		//不显示子菜单
		if(!bl&&table_dictCity.isDisplayed())table_data.click();
	}
	
	//城市字典
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[7]/ul/li[1]")
	WebElement table_dictCity;
	public void table_dictCityClick(){
		table_dictCity.click();
	}
	
	//版本类型字典
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[7]/ul/li[3]")
	WebElement table_dictProduct;
	public void table_dictProductClick(){
		table_dictProduct.click();
	}
	
	//产品类型字典
	@FindBy(how = How.XPATH, xpath = "/html/body/section/div[1]/div/ul/li[7]/ul/li[2]")
	WebElement table_dictVersion;
	public void table_dictVersionClick(){
		table_dictVersion.click();
	}
	
	
	//输入用户名、密码、验证码
	public void inputUserInfo(String userName,String passWord,String key) {
		edit_UserName.clear();
		edit_UserName.click();
		edit_UserName.sendKeys(userName);
		edit_Password.clear();
		edit_Password.click();
		edit_Password.sendKeys(passWord);
		edit_Key.clear();
		edit_Key.click();
		edit_Key.sendKeys(key);
	}
	
	//点击登陆按钮
	public void clickBtnLogin(){
		btn_login.click();
	}
	
	//退出登录
	public void exitUsername(int wait){
		//等待页加载
		WebElement userName=Operate.getClickWebElement(driver,"//div[2]/div/div/div/span[1]","xpath",wait);
		ThreadTime.sleepTime(1);
		userName.click();
		
		WebElement ele=Operate.getClickWebElement(driver,"//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']","xpath",wait);
		ThreadTime.sleepTime(1);
		ele.click();
		
		WebElement btn_determine=Operate.getClickWebElement(driver,"//div[@class='el-message-box__btns']/button[2]","xpath",wait);
		ThreadTime.sleepTime(1);
		btn_determine.click();
		
	}
	
	//界面选择英文
	public boolean choiceEnglish() {
		btn_language_English.click();
		return true;
	}
	
	//界面选择中文
	public void btn_language_chinese() {
		btn_language_chinese.click();
	}

	//等待number S检查是否弹出sucess
	public boolean isMsgSucess(int number){
		return Operate.getWaitIsDisplay(driver,"//div[@class='el-message__group']","xpath",number);
	}
	
	//返回用户名
	public String getUserName(){
		return btn_userName.getText();
	}
	
	
	public void getUrl(String ieUrl) {
		String CurrentUrl = driver.getCurrentUrl();
		if (!driver.getCurrentUrl().equals(ieUrl)) {
			log.error("URL Error!" + "CurrentUrl=" + CurrentUrl + "   targetURL=" + ieUrl);
			driver.get(ieUrl);
		}
	}
}
